/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.dao.jpa;

import com.supinfo.youfood.dao.CommandeDao;
import com.supinfo.youfood.entity.Commande;
import com.supinfo.youfood.entity.Employe;
import com.supinfo.youfood.entity.Restaurant;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Michael
 */
@Stateless
public class JpaCommandeDao implements CommandeDao{
    
    @PersistenceContext
    private EntityManager em;
    
    @Override
    public void creerCommande(Commande c) {
        em.persist(c);
    }
    
    @Override
    public void supprimerCommande(Commande c) {
        em.find(Commande.class, c);
        em.remove(c);
    }
    
    @Override
    public List<Commande> getAllCommandes() {
        String query = "SELECT c "+
                        "FROM Commande c";
        
        Query result = em.createQuery(query);
        List<Commande> res = (List<Commande>)result.getResultList();
        return res;
    }
    
    @Override
    public List<Commande> getAllCommandesByEmp (Employe e) {
        String query = "SELECT c "+
                        "FROM Commande c "+
                        "WHERE c.serveur = :serveur";
        
        Query result = em.createQuery(query);
        result.setParameter("serveur", e);
        List<Commande> res = (List<Commande>)result.getResultList();
        return res;
    }

    @Override
    public Commande findCommandeById(Long id) {
        return em.find(Commande.class, id);
    }

    @Override
    public List<Commande> getAllCommandesByResto(Restaurant res) {
        String query = "SELECT c "+
                        "FROM Commande c "+
                        "WHERE c.tableResto.resto = :res";
        Query result = em.createQuery(query);
        result.setParameter("res", res);
        return (List<Commande>)result.getResultList();
    }

    @Override
    public void updateCommande(Commande c) {
        em.merge(c);
    }

}
